﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingUpperLimb
{
    public partial class AddUpperLimb : Form
    {
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id;
        public AddUpperLimb(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            this.Id = id;
            BindInfo(id);
        }
        public AddUpperLimb(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            BindInfo(this.Id);
        }
        public AddUpperLimb(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            BindInfo(this.Id);
        }

        private void BindInfo(string id)
        {
            dateriqi.DateTime = DateTime.Now;   //显示当前日期
            if (string.IsNullOrEmpty(id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            UpperLimb ps = new UpperLimb();
            ps = XmlUtil.XMLToObject<UpperLimb>(NursingFormData, Encoding.UTF8);
            dateriqi.Text = ps.ZhiGuanJiLu.PICCZhiGuanShiJian;
            txtCaozuozhe.Text = ps.ZhiGuanJiLu.PICCCaoZhe;
            txtzhushou.Text = ps.ZhiGuanJiLu.ZhuShou;
            txtDaoguanleixing.Text = ps.ZhiGuanJiLu.DaoGuanLeiXing;
            txtguigexinghao.Text = ps.ZhiGuanJiLu.GuiGeXingHao;
            txtTiaoxingmapihao.Text = ps.ZhiGuanJiLu.TiaoXingMaPiHao;
            txtdaoruxiaoleixing.Text = ps.ZhiGuanJiLu.DaoRuXiaoLeiXing;
            txtdaoruxiaoguige.Text = ps.ZhiGuanJiLu.WeiXingDaoRuXiaoGuiGe;
            rdosChuancibweishang.SelectedIndex = Convert.ToInt32(string.IsNullOrEmpty(ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiZuo) ? "-1" : ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiZuo);
            rdosChuancibweishang.SelectedIndex = Convert.ToInt32(string.IsNullOrEmpty(ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiYou) ? "-1" : ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiYou);
            rdosChuancibweixia.SelectedIndex = Convert.ToInt32(string.IsNullOrEmpty(ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiZuo) ? "-1" : ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiZuo);
            rdosChuancibweixia.SelectedIndex = Convert.ToInt32(string.IsNullOrEmpty(ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiYou) ? "-1" : ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiYou);
            txtChuancibuwxiaQt.Text = ps.ZhiGuanJiLu.ChuanCiBuWeiQiTa;
            txtCharuchangdu.Text = ps.ZhiGuanJiLu.ChaRuChangDu;
            txtwailcdu.Text = ps.ZhiGuanJiLu.WaiLuChangDu;
            txtjianqcdu.Text = ps.ZhiGuanJiLu.JianQuChangDu;
            txtxdingwei.Text = ps.ZhiGuanJiLu.XDingWei;
            txtercidingw.Text = ps.ZhiGuanJiLu.ErCiDingWei;
            txtzaituichu.Text = ps.ZhiGuanJiLu.ZaiTuiChu;
            txtShijcdu.Text = ps.ZhiGuanJiLu.ShiJiChaRuChangDu;
            txtjianduanwzhi.Text = ps.ZhiGuanJiLu.XinDeJianDuanWeiZhi;
            txtcaozzhe.Text = ps.ZhiGuanJiLu.CaoZuoZhe;
            txtZhiguanjguo.Text = ps.ZhiGuanJiLu.ZhiGunJingGuo;
            if (!string.IsNullOrEmpty(ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian))
            {
                string[] date1 = ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/');
                dateEdit1.Text = date1[0] + "年" + date1[1] + "月" + date1[2] + "日" + date1[3] + "时";
            }
            if (!string.IsNullOrEmpty(ps.BaGuanJiLu.RiQi))
            {
                string[] date2 = ps.BaGuanJiLu.RiQi.Split('/');
                dateEdit2.Text = date2[0] + "年" + date2[1] + "月" + date2[2] + "日" + date2[3] + "时";
            }

            txtBaguanCCZ.Text = ps.BaGuanJiLu.CaoZuoZhe;
            txtBaguanJingg.Text = ps.BaGuanJiLu.BaGuanJingGuo;

            txtBaguanyy1.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[0].Equals("1");
            txtBaguanyy2.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[1].Equals("1");
            txtBaguanyy3.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[2].Equals("1");
            txtBaguanyy4.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[3].Equals("1");
            txtBaguanyy5.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[4].Equals("1");
            txtBaguanyy6.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[5].Equals("1");
            txtBaguanyy7.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[6].Equals("1");
            txtBaguanyy8.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[7].Equals("1");
            txtBaguanyy9.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[8].Equals("1");
            txtBaguanyy10.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[9].Equals("1");
            txtBaguanyy11.Checked = ps.BaGuanJiLu.BaGuanYuanYin.Split('#')[10].Equals("1");
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateriqi.Text.Trim()))
                dateriqi.DateTime = DateTime.Now;

            UpperLimb ps = new UpperLimb();
            //ps doing...          
            ps.FormId = (int)(FormType.UpperLimbNursing);
            ps.FormName = EnumHelper.GetDescription(FormType.UpperLimbNursing);

            ps.ZhiGuanJiLu.PICCZhiGuanShiJian = dateriqi.Text;
            ps.ZhiGuanJiLu.PICCCaoZhe = txtCaozuozhe.Text;
            ps.ZhiGuanJiLu.ZhuShou = txtzhushou.Text;
            ps.ZhiGuanJiLu.DaoGuanLeiXing = txtDaoguanleixing.Text;

            ps.ZhiGuanJiLu.GuiGeXingHao = txtguigexinghao.Text;
            ps.ZhiGuanJiLu.TiaoXingMaPiHao = txtTiaoxingmapihao.Text;
            ps.ZhiGuanJiLu.DaoRuXiaoLeiXing = txtdaoruxiaoleixing.Text;
            ps.ZhiGuanJiLu.WeiXingDaoRuXiaoGuiGe = txtdaoruxiaoguige.Text;
            ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiZuo = rdosChuancibweishang.SelectedIndex.ToString();
            ps.ZhiGuanJiLu.ChuanCiBuWeiShangZhiYou = rdosChuancibweishang.SelectedIndex.ToString();
            ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiZuo = rdosChuancibweixia.SelectedIndex.ToString();
            ps.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiYou = rdosChuancibweixia.SelectedIndex.ToString();
            ps.ZhiGuanJiLu.ChuanCiBuWeiQiTa = txtChuancibuwxiaQt.Text;
            ps.ZhiGuanJiLu.ChaRuChangDu = txtCharuchangdu.Text;
            ps.ZhiGuanJiLu.WaiLuChangDu = txtwailcdu.Text;
            ps.ZhiGuanJiLu.JianQuChangDu = txtjianqcdu.Text;
            ps.ZhiGuanJiLu.XDingWei = txtxdingwei.Text;
            ps.ZhiGuanJiLu.ErCiDingWei = txtercidingw.Text;
            ps.ZhiGuanJiLu.ZaiTuiChu = txtzaituichu.Text;
            ps.ZhiGuanJiLu.ShiJiChaRuChangDu = txtShijcdu.Text;
            ps.ZhiGuanJiLu.XinDeJianDuanWeiZhi = txtjianduanwzhi.Text;
            ps.ZhiGuanJiLu.CaoZuoZhe = txtcaozzhe.Text;
            ps.ZhiGuanJiLu.ZhiGunJingGuo = txtZhiguanjguo.Text;
            ps.ZhiGuanJiLu.DaiGuanChuYuan = chkDaiguancy.Checked ? "1" : "0";
            ps.ZhiGuanJiLu.DaiGuanChuYuanShiJian = dateEdit1.DateTime.Year.ToString() + "/" + dateEdit1.DateTime.Month.ToString() + "/" + dateEdit1.DateTime.Day.ToString() + "/" + dateEdit1.DateTime.Hour.ToString();
            ps.BaGuanJiLu.CaoZuoZhe = txtBaguanCCZ.Text;
            ps.BaGuanJiLu.RiQi = dateEdit2.DateTime.Year.ToString() + "/" + dateEdit2.DateTime.Month.ToString() + "/" + dateEdit2.DateTime.Day.ToString() + "/" + dateEdit2.DateTime.Hour.ToString();
            ps.BaGuanJiLu.BaGuanJingGuo = txtBaguanJingg.Text;

            ps.BaGuanJiLu.BaGuanYuanYin =
                (txtBaguanyy1.Checked ? 1 : 0) + "#" +
                (txtBaguanyy2.Checked ? 1 : 0) + "#" +
                (txtBaguanyy3.Checked ? 1 : 0) + "#" +
                (txtBaguanyy4.Checked ? 1 : 0) + "#" +
                (txtBaguanyy5.Checked ? 1 : 0) + "#" +
                (txtBaguanyy6.Checked ? 1 : 0) + "#" +
                (txtBaguanyy7.Checked ? 1 : 0) + "#" +
                (txtBaguanyy8.Checked ? 1 : 0) + "#" +
                (txtBaguanyy9.Checked ? 1 : 0) + "#" +
                (txtBaguanyy10.Checked ? 1 : 0) + "#" +
                (txtBaguanyy11.Checked ? 1 : 0);


            record.NursingFormData = XmlUtil.ObjectToXML(ps, Encoding.UTF8);
            record.FormId = ps.FormId;//表单ID
            record.FormName = ps.FormName;//表单NAME
            record.PatientId = PatientInfo.PATIENT_ID;//住院号
            record.VisitId = PatientInfo.VISIT_ID;//住院次数
            record.PatientName = PatientInfo.PATIENT_NAME;//患者名称
            record.BedNo = PatientInfo.BED_NO;//床号
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;//科室号
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;//科室名称
            record.AreaId = PatientInfo.NURSE_CELL_CODE;//病区编号
            record.AreaName = PatientInfo.NURSE_CELL_NAME;//病区名称
            record.AssessmentDate = dateriqi.DateTime;
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
